home *** CD-ROM | disk | FTP | other *** search
JavaScript | 1997-09-21 | 17.8 KB | 673 lines | [TEXT/LMAN] |
- //***************************************
- //* musicObject code 1.6
- //* written by C. van Rensburg
- //*
- //* © Copyright 1996,1997 Headspace, Inc.
- //* All Rights Reserved
- //***************************************
-
- function mo_play (p1,p2) {
- if (this.ready) {
- clearTimeout (this.fadeTimeout);
- if ((typeof (p1) == "boolean" || typeof (p1) == "number") && typeof (p2) == "string") {
- document [this.pluginName].play (p1,p2);
- } else if (typeof (p1) == "string" || typeof (p1) == "boolean" || typeof (p1) == "number") {
- document [this.pluginName].play (p1);
- } else {
- document [this.pluginName].play ();
- }
- }
- }
-
- function mo_playGroovoid (p1,p2) {
- this.play (p1,"groovoid://" + p2);
- }
-
- function mo_stop (fade) {
- with (this) {
- if (ready) {
- clearTimeout (fadeTimeout);
- if (typeof (fade) != "boolean") {
- document [pluginName].stop ();
- } else {
- document [pluginName].stop (fade);
- }
- }
- }
- }
-
- function mo_pauseAtZero () {
- with (this) {
- if (ready) {
- if (getVolume () == 0) {
- pause ();
- clearTimeout (fadeTimeout);
- } else {
- pauseTimeout = setTimeout (name + '.pauseAtZero ()',500);
- }
- }
- }
- }
-
- function mo_pause (fade) {
- with (this) {
- if (ready) {
- if (typeof (fade) != "boolean") {
- clearTimeout (fadeTimeout);
- document [pluginName].pause ();
- } else if (fade) {
- fadeFromTo (getVolume (),0);
- pauseAtZero ();
- } else {
- clearTimeout (fadeTimeout);
- document [pluginName].pause ();
- }
- }
- }
- }
-
- function mo_setLoop (state) {
- if (this.ready) {document [this.pluginName].setLoop (state)}
- }
-
- function mo_setAutostart (state) {
- if (this.ready) {document [this.pluginName].setAutostart (state)}
- }
-
- function mo_fadeTo (toValue) {
- if (this.ready) {document [this.pluginName].fade_to (toValue)}
- }
-
- function mo_fadeFromTo (fromValue,toValue) {
- if (this.ready) {document [this.pluginName].fade_from_to (fromValue,toValue)}
- }
-
- function mo_setReady (initFunction) {
- with (this) {
- if (document [pluginName] != null) {
- ready = true;
- eval (initFunction);
- } else {
- setTimeout (name + '.setReady ("' + initFunction + '")',500);
- }
- }
- }
-
- function mo_setMonophonic (channelNo,state) {
- with (this) {
- if (channelNo == 0) {
- for (var channelCount = 1; channelCount <= 16; channelCount++) {
- monophonic [channelCount-1] = state;
- }
- } else {
- monophonic [channelNo-1] = state;
- }
- }
- }
-
- function mo_getMonophonic (channelNo) {
- with (this) {
- if (channelNo >= 1 && channelNo <= 16) {
- return monophonic [channelNo-1];
- } else {
- return false;
- }
- }
- }
-
- function mo_noteOn (channelNo,p2,p3,p4,p5) {
- with (this) {
- if (ready) {
- if (monophonic [channelNo-1]) {
- noteOff (channelNo,notesOn [channelNo],127);
- }
- if (p4 != null && p5 != null) {
- notesOn [channelNo] = p4;
- document [pluginName].noteOn (channelNo,p2,p3,p4,p5);
- } else {
- notesOn [channelNo] = p2;
- document [pluginName].noteOn (channelNo,p2,p3);
- }
- }
- }
- }
-
- function mo_noteOff (channelNo,noteNo) {
- with (this) {
- if (ready) {
- notesOn [channelNo] = 0;
- document [pluginName].noteOff (channelNo,noteNo,127)
- }
- }
- }
-
- function mo_playNote (channelNo,p2,p3,p4,p5,p6) {
- with (this) {
- if (ready) {
- if (monophonic [channelNo-1]) {
- noteOff (channelNo,notesOn [channelNo],127);
- }
- if (p5 != null & p6 != null) {
- noteOn (channelNo,p2,p3,p4,p5,p6);
- setTimeout (this.name + '.noteOff (' + channelNo + ',' + p4 + ',' + p5 + ')',p6);
- // notesOn [channelNo] = p4;
- // setProgram (channelNo,p2,p3);
- // document [this.pluginName].playNote (channelNo,p4,p5,p6);
- } else {
- noteOn (channelNo,p2,p3,p4);
- setTimeout (this.name + '.noteOff (' + channelNo + ',' + p2 + ',' + p3 + ')',p4);
- // notesOn [channelNo] = p2;
- // document [this.pluginName].playNote (channelNo,p2,p3,p4);
- }
- }
- }
- }
-
- function mo_playSample (sampleName,pan,volume) {
- if (this.ready) {document [this.pluginName].playSample (sampleName,pan,volume)}
- }
-
- function mo_setGlobalMute (muteState) {
- if (this.ready) {document [this.pluginName].setGlobalMute (muteState)}
- }
-
- function mo_setPanelDisplay (displayType) {
- if (this.ready) {document [this.pluginName].setPanelDisplay (displayType)}
- }
-
- function mo_setPanelMode (panelMode) {
- if (this.ready) {document [this.pluginName].setPanelMode (panelMode)}
- }
-
- function mo_setVolume (volume) {
- if (this.ready) {document [this.pluginName].setVolume (volume)}
- }
-
- function mo_setTranspose (transpose) {
- if (this.ready) {document [this.pluginName].setTranspose (transpose)}
- }
-
- function mo_setTempo (tempo) {
- if (this.ready) {document [this.pluginName].setTempo (tempo)}
- }
-
- function mo_setReverbType (reverbType) {
- if (this.ready) {document [this.pluginName].setReverbType (reverbType)}
- }
-
- function mo_setController (channelNo,controllerNo,controllerValue) {
- if (this.ready) {document [this.pluginName].setController (channelNo,controllerNo,controllerValue)}
- }
-
- function mo_setProgram (channelNo,p2,p3) {
- if (this.ready) {
- with (this) {
- if (p3 != null) {
- document [this.pluginName].setProgram (channelNo,p2, p3);
- } else {
- document [this.pluginName].setProgram (channelNo,p2);
- }
- }
- }
- }
-
- function mo_setTrackMute (trackNo,state) {
- if (this.ready) {document [this.pluginName].setTrackMute (trackNo,state)}
- }
-
- function mo_setChannelMute (channelNo,state) {
- if (this.ready) {document [this.pluginName].setChannelMute (channelNo,state)}
- }
-
- function mo_setTrackSolo (trackNo,state) {
- if (this.ready) {document [this.pluginName].setTrackSolo (trackNo,state)}
- }
-
- function mo_setChannelSolo (channelNo,state) {
- if (this.ready) {document [this.pluginName].setChannelSolo (channelNo,state)}
- }
-
- function mo_getPanelDisplay () {
- if (this.ready) {return document [this.pluginName].getPanelDisplay ()} else {return ""}
- }
-
- function mo_getPanelMode (panelMode) {
- if (this.ready) {return document [this.pluginName].getPanelMode ()} else {return ""}
- }
-
- function mo_getVolume () {
- if (this.ready) {return document [this.pluginName].GetVolume ()} else {return 100}
- }
-
- function mo_getAutostart () {
- if (this.ready) {return document [this.pluginName].getAutostart ()} else {return false}
- }
-
- function mo_getLoop () {
- if (this.ready) {return document [this.pluginName].getLoop ()} else {return false}
- }
-
- function mo_getReverbType () {
- if (this.ready) {return document [this.pluginName].getReverbType ()} else {return 0}
- }
-
- function mo_getTempo () {
- if (this.ready) {return document [this.pluginName].getTempo ()} else {return 120}
- }
-
- function mo_getTranspose () {
- if (this.ready) {return document [this.pluginName].getTranspose ()} else {return 0}
- }
-
- function mo_getController (channelNo,controllerNo) {
- if (this.ready) {return document [this.pluginName].getController (channelNo,controllerNo)} else {return 0}
- }
-
- function mo_getProgram (channelNo) {
- if (this.ready) {return document [this.pluginName].getProgram (channelNo)} else {return 0}
- }
-
- function mo_getTrackMute (trackNo) {
- if (this.ready) {return document [this.pluginName].getTrackMute (trackNo)} else {return false}
- }
-
- function mo_getChannelMute (channelNo) {
- if (this.ready) {return document [this.pluginName].getChannelMute (channelNo)} else {return false}
- }
-
- function mo_getTrackSolo (trackNo) {
- if (this.ready) {return document [this.pluginName].getTrackSolo (trackNo)} else {return false}
- }
-
- function mo_getChannelSolo (channelNo) {
- if (this.ready) {return document [this.pluginName].getChannelSolo (channelNo)} else {return false}
- }
-
- function mo_getInfo (infoField) {
- if (this.ready) {return document [this.pluginName].getInfo (infoField)} else {return ''}
- }
-
- function mo_isPaused () {
- if (this.ready) {return document [this.pluginName].IsPaused ()} else {return false}
- }
-
- function mo_isPlaying () {
- if (this.ready) {return document [this.pluginName].IsPlaying ()} else {return false}
- }
-
- function mo_isReady () {
- return this.ready;
- }
-
- function mo_getNoteNumber (noteName) {
- noteOffset = this.noteNumbers [noteName.substring (0,1).toLowerCase ()];
- if (noteOffset == null) {
- return 0;
- } else {
- sharpFlatOffset = 0;
- sharpFlatPos = noteName.indexOf ('b',1);
- if (sharpFlatPos == -1) {
- sharpFlatPos = noteName.indexOf ('#',1);
- if (sharpFlatPos == -1) {
- sharpFlatPos = 0;
- } else {
- sharpFlatOffset = 1;
- }
- } else {
- sharpFlatOffset = -1;
- }
- octaveNo = noteName.substring (sharpFlatPos+1,noteName.length) - 0;
- return 12 + octaveNo * 12 + noteOffset + sharpFlatOffset;
- }
- }
-
- function mo_getNoteName (noteNumber) {
- return this.noteNames [noteNumber % 12] + (Math.floor (noteNumber / 12) - 1) + "";
- }
-
- function mo_getVersion () {
- return ('1.5');
- }
-
- function mo_getPlatform () {
- return (navigator.userAgent);
- }
-
- function voice_define (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal) {
- this.bankNo = bankNo;
- this.programNo = programNo;
- this.noteNo = noteNo;
- this.velocityVal = velocityVal;
- this.volumeVal = volumeVal;
- this.panVal = panVal;
- this.flagged = false;
- }
-
- function voice (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal) {
- this.bankNo = bankNo;
- this.programNo = programNo;
- this.noteNo = noteNo;
- this.velocityVal = velocityVal;
- this.volumeVal = volumeVal;
- this.panVal = panVal;
- this.flagged = false;
- this.define = voice_define;
- }
-
- function mo_sceneStart () {
- with (this) {
- totalNewVoices = 0;
- }
- }
-
- function mo_sceneCheckVoiceAllocation (newVoiceNo) {
- with (this) {
- for (voiceNo = 1; voiceNo <= 16; voiceNo++) {
- if (voices [voiceNo-1] != null) {
- if (
- newVoices [newVoiceNo-1].bankNo == voices [voiceNo-1].bankNo &&
- newVoices [newVoiceNo-1].programNo == voices [voiceNo-1].programNo &&
- newVoices [newVoiceNo-1].noteNo == voices [voiceNo-1].noteNo &&
- newVoices [newVoiceNo-1].velocityVal == voices [voiceNo-1].velocityVal
- ) {
- voices [voiceNo-1].flagged = true;
- newVoices [newVoiceNo-1].flagged = true;
- voices [voiceNo-1].volumeVal = newVoices [newVoiceNo-1].volumeVal;
- voices [voiceNo-1].panVal = newVoices [newVoiceNo-1].panVal;
- setController (voiceNo,7,voices [voiceNo-1].volumeVal);
- setController (voiceNo,10,voices [voiceNo-1].panVal);
- }
- }
- }
- }
- }
-
- function mo_sceneAllocateVoice (newVoiceNo) {
- with (this) {
- if (!newVoices [newVoiceNo-1].flagged) {
- for (voiceNo = 1; voiceNo <= 16; voiceNo++) {
- with (newVoices [newVoiceNo-1]) {
- if (!flagged) {
- voices [voiceNo-1].define (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal);
- flagged = true;
- setController (voiceNo,7,volumeVal);
- setController (voiceNo,10,panVal);
- noteOn (voiceNo,bankNo,programNo,noteNo,velocityVal);
- break;
- }
- }
- }
- }
- }
- }
-
- function mo_sceneEnd () {
- with (this) {
- for (voiceNo = 1; voiceNo <= 16; voiceNo++) {
- voices [voiceNo-1].flagged = false;
- }
- for (newVoiceNo = 1; newVoiceNo <= totalNewVoices; newVoiceNo++) {
- sceneCheckVoiceAllocation (newVoiceNo);
- }
- for (voiceNo = 1; voiceNo <= 16; voiceNo++) {
- with (voices [voiceNo-1]) {
- if (!flagged && bankNo != -1) {
- noteOff (voiceNo,noteNo,velocityVal);
- bankNo = -1;
- }
- }
- }
- for (newVoiceNo = 1; newVoiceNo <= totalNewVoices; newVoiceNo++) {
- sceneAllocateVoice (newVoiceNo);
- }
- }
- }
-
- function mo_sceneVoice (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal) {
- with (this) {
- totalNewVoices++;
- newVoices [totalNewVoices-1].define (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal);
- }
- }
-
- function mo_sceneImmediateVoice (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal) {
- with (this) {
- sceneVoice (bankNo,programNo,noteNo,velocityVal,volumeVal,panVal);
- sceneCheckVoiceAllocation (totalNewVoices);
- sceneAllocateVoice (totalNewVoices);
- totalNewVoices--;
- }
- }
-
- function musicObject (name) {
- this.fadeTimeout = setTimeout ('//',0);
-
- this.name = name;
- this.pluginName = name + "Plugin";
- this.ready = false;
-
- //****************************
- // reflected plug-in functions
-
- this.play = mo_play;
- this.playGroovoid = mo_playGroovoid;
- this.stop = mo_stop;
- this.pause = mo_pause;
- this.setLoop = mo_setLoop;
- this.fadeTo = mo_fadeTo;
- this.fade_to = mo_fadeTo;
- this.fadeFromTo = mo_fadeFromTo;
- this.fade_from_to = mo_fadeFromTo;
- this.setAutostart = mo_setAutostart;
-
- this.playNote = mo_playNote;
- this.noteOn = mo_noteOn;
- this.noteOff = mo_noteOff;
- this.playSample = mo_playSample;
-
- this.setPanelDisplay = mo_setPanelDisplay;
- this.setPanelMode = mo_setPanelMode;
- this.setGlobalMute = mo_setGlobalMute;
- this.setVolume = mo_setVolume;
- this.setTranspose = mo_setTranspose;
- this.setController = mo_setController;
- this.setProgram = mo_setProgram;
- this.setTempo = mo_setTempo;
- this.setReverbType = mo_setReverbType;
- this.setTrackMute = mo_setTrackMute;
- this.setChannelMute = mo_setChannelMute;
- this.setTrackSolo = mo_setTrackSolo;
- this.setChannelSolo = mo_setChannelSolo;
-
- this.getPanelDisplay = mo_getPanelDisplay;
- this.getPanelMode = mo_getPanelMode;
- this.getVolume = mo_getVolume;
- this.GetVolume = mo_getVolume;
- this.getAutostart = mo_getAutostart;
- this.getLoop = mo_getLoop;
- this.getReverbType = mo_getReverbType;
- this.getTempo = mo_getTempo;
- this.getTranspose = mo_getTranspose;
- this.getController = mo_getController;
- this.getProgram = mo_getProgram;
- this.getTrackMute = mo_getTrackMute;
- this.getChannelMute = mo_getChannelMute;
- this.getTrackSolo = mo_getTrackSolo;
- this.getChannelSolo = mo_getChannelSolo;
- this.getInfo = mo_getInfo;
- this.isPaused = mo_isPaused;
- this.IsPaused = mo_isPaused;
- this.isPlaying = mo_isPlaying;
- this.IsPlaying = mo_isPlaying;
- this.isReady = mo_isReady;
- this.IsReady = mo_isReady;
-
- //******** extended authoring API functions *********
-
- this.setReady = mo_setReady;
- this.setMonophonic = mo_setMonophonic;
- this.getMonophonic = mo_getMonophonic;
- this.playNotePlus = mo_playNote;
- this.pauseAtZero = mo_pauseAtZero;
- this.getNoteNumber = mo_getNoteNumber;
- this.getNoteName = mo_getNoteName;
- this.getVersion = mo_getVersion;
- this.getPlatform = mo_getPlatform;
-
- this.sceneCheckVoiceAllocation = mo_sceneCheckVoiceAllocation;
- this.sceneAllocateVoice = mo_sceneAllocateVoice;
- this.sceneStart = mo_sceneStart;
- this.sceneVoice = mo_sceneVoice;
- this.sceneImmediateVoice = mo_sceneImmediateVoice;
- this.sceneEnd = mo_sceneEnd;
-
- //***************************************************
-
- this.monophonic = new Array (false,false,false,false,false,false,false,false,false,false,false,false,false,false,false);
- this.notesOn = new Array (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
-
- this.noteNumbers = new Array ();
- this.noteNumbers ['c'] = 0;
- this.noteNumbers ['d'] = 2;
- this.noteNumbers ['e'] = 4;
- this.noteNumbers ['f'] = 5;
- this.noteNumbers ['g'] = 7;
- this.noteNumbers ['a'] = 9;
- this.noteNumbers ['b'] = 11;
-
- this.noteNames = new Array ("C","C#","D","D#","E","F","F#","G","G#","A","A#","B");
-
- this.voices = new Array ();
- this.newVoices = new Array ();
- for (voiceNo = 1; voiceNo <= 16; voiceNo++) {
- this.voices [voiceNo-1] = new voice (0,0,0,0,0,0);
- this.newVoices [voiceNo-1] = new voice (0,0,0,0,0,0);
- }
- this.totalNewVoices = 0;
- }
-
- //***********
- //* random ()
-
- today = new Date ();
-
- function random (range) {
- return (Math.round (today.getTime () / 100) % range) + 1;
- }
-
- //***********************
- //* randomPP object class
-
- function randomPP_selectNext () {
- flagsFound = 0;
- with (Math) {randomFlag = round (random () * (this.flagsRemaining - 1)) + 1}
- for (var flagNo = 1; flagNo <= this.totalFlags; flagNo++) {
- if (!this.flags [flagNo]) {
- flagsFound++;
- if (flagsFound == randomFlag) {
- randomFlag = flagNo;
- this.flags [randomFlag] = true;
- break;
- }
- }
- }
- this.flagsSelected++;
- this.flagsRemaining--;
- if (this.flagsSelected == this.totalFlags) {
- this.flagsSelected = 0;
- this.flagsRemaining = this.totalFlags;
- for (var flagNo = 1; flagNo <= this.totalFlags; flagNo++) {
- this.flags [flagNo] = false;
- }
- }
- return randomFlag;
- }
-
- function randomPP (range) {
- this.flags = new Array (range)
- this.totalFlags = range;
- this.flagsSelected = 0;
- this.flagsRemaining = this.totalFlags;
- this.selectNext = randomPP_selectNext;
- for (var flagNo = 1; flagNo <= this.totalFlags; flagNo++) {
- this.flags [flagNo] = false;
- }
- }
-
- //*******************
- //* randomFromList ()
-
- function randomFromList (prefix,suffix) {
- totalItems = randomFromList.arguments.length - 2;
- if (totalItems == 0) {
- return prefix + suffix;
- } else {
- randomItemNo = random (totalItems);
- return prefix + randomFromList.arguments [randomItemNo + 1] + suffix;
- }
- }
-
- //*************
- //* mimeType ()
-
- extMIME = new Array ();
-
- extMIME ['mid'] = 'audio/midi';
- extMIME ['midi'] = 'audio/midi';
- extMIME ['rmf'] = 'audio/rmf';
- extMIME ['wav'] = 'audio/wav';
- extMIME ['au'] = 'audio/basic';
- extMIME ['aif'] = 'audio/aiff';
- extMIME ['aiff'] = 'audio/aiff';
- extMIME ['mod'] = 'audio/mod';
- extMIME ['m15'] = 'audio/mod';
- extMIME ['mtm'] = 'audio/mod';
- extMIME ['s3m'] = 'audio/mod';
- extMIME ['ult'] = 'audio/mod';
- extMIME ['xm'] = 'audio/mod';
-
- function mimeType (fileName) {
- returnVal = '';
- delimPos = fileName.lastIndexOf ('.');
- if (delimPos != -1) {
- returnVal = extMIME [fileName.substring (delimPos + 1,fileName.length)];
- if (returnVal == null) {returnVal = ''}
- }
- return returnVal;
- }
-
- //***************
- //* hasBeatnik ()
-
- hasBeatnikFlag = null;
-
- function hasBeatnik () {
- if (hasBeatnikFlag == null) {
- hasBeatnikFlag = true;
- with (navigator) {
- if (userAgent.indexOf ('Mozilla/3.0') != -1 || userAgent.indexOf ('Mozilla/4.0') != -1) {
- hasBeatnikFlag = false;
- for (pluginNo = 1; pluginNo <= plugins.length && !hasBeatnikFlag; pluginNo++) {
- if (plugins [pluginNo-1].name.indexOf ('Beatnik') != -1) {
- hasBeatnikFlag = true;
- }
- }
- }
- }
- }
- return hasBeatnikFlag;
- }
-
- //***************
- //* magicEmbed ()
-
- function magicEmbed (embedURL) {
- with (document) {
- if (hasBeatnik ()) {
- writeln ('<EMBED SRC="' + embedURL + '" TYPE="audio/rmf" WIDTH=144 HEIGHT=60 AUTOSTART=TRUE LOOP=TRUE>');
- } else {
- writeln ('<A HREF="http://www.headspace.com/beatnik/?plug-in" TARGET="_top"><IMG SRC="http://www.headspace.com/beatnik/images/get-beatnik-plugin.gif" WIDTH=144 HEIGHT=60 BORDER=0 ALT="Get the Beatnik Plug-in NOW!"></A>');
- }
- }
- }
-